
package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.HospitalNotice;
import com.scs.application.modules.base.mapper.HospitalNoticeMapper;
import com.scs.application.modules.base.request.HospitalNoticeQueryRequest;
import com.scs.application.modules.base.service.HospitalNoticeService;
import com.scs.application.modules.msg.dto.MscmReceiveDTO;
import com.scs.application.modules.msg.service.MscmReceiveService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 群发供应商 service 实现
 */
@Slf4j
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class HospitalNoticeServiceImpl extends BaseServiceImpl<HospitalNoticeMapper, HospitalNotice> implements HospitalNoticeService {

    private final MscmReceiveService mscmReceiveService;

    @Override
    public List<HospitalNotice> list(HospitalNoticeQueryRequest request) {
        List<HospitalNotice> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<HospitalNotice> page(HospitalNoticeQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    @Override
    public Boolean saveMsg(HospitalNotice entity) {
        return this.saveOrUpdate(entity);
    }

    @Override
    public void updateIsSendById(HospitalNotice entity) {
        HospitalNotice hospitalNotice = new HospitalNotice();
        hospitalNotice.setIsSend("1").setId(entity.getId());
        this.updateById(hospitalNotice);
        //插入到msg 消息记录表和接收表
        MscmReceiveDTO dto = new MscmReceiveDTO().setMsgType(entity.getMsgType())
                .setTitle(entity.getTitle())
                .setContent(entity.getContent())
                .setIsRead("0")
                .setIsDeleted("0");

        mscmReceiveService.saveSendToSupplier(dto);
    }

    private QueryWrapper<HospitalNotice> getQueryWrapper(HospitalNoticeQueryRequest request) {
        return Wrappers.<HospitalNotice>query()
                .eq("msg_type", request.getMsgType())
                .like(StringUtils.isNotBlank(request.getTitle()), "title", request.getTitle())
                .orderByDesc("gmt_modified");
    }
}
